#include <cstdlib>
using namespace std;

#include "ParticleEmitter.hpp"
#include "our_math.h"

ParticleEmitter::ParticleEmitter(ParticleSystem *system,
                                 float x, float y,
                                 float angle, float angle_deviation,
                                 float power, float power_deviation,
                                 float scale, float scale_deviation,
                                 float rotation_speed, float rotation_speed_deviation,
                                 float mass, float mass_deviation,
                                 float dt_scale, float dt_scale_deviation,
                                 unsigned int particles_per_second) :
        system(system), x(x), y(y),
        angle(angle, angle_deviation),
        power(power, power_deviation),
        scale(scale, scale_deviation),
        rotation_speed(rotation_speed, rotation_speed_deviation),
        mass(mass, mass_deviation),
        dt_scale(dt_scale, dt_scale_deviation),
        particles_per_second(particles_per_second) {
}

void ParticleEmitter::update() {
    float particles_per_frame = particles_per_second * dt;
    static float error = 0;
    error += particles_per_frame;

    for (int i = 0; i < static_cast<int>(error); i++) {
        float particle_angle = angle.get_ranged_value();
        float particle_power = power.get_ranged_value();

        float x_speed = sin(particle_angle)*particle_power;
        float y_speed = cos(particle_angle)*particle_power;

        system->particles.push_back(new Particle(x, y,
                                    x_speed, y_speed,
                                    particle_angle,
                                    rotation_speed.get_ranged_value(),
                                    scale.get_ranged_value(),
                                    mass.get_ranged_value(),
                                    dt_scale.get_ranged_value()));
    }

    error -= static_cast<int>(error);
}

void ParticleEmitter::output(){
    cout << "ParticleEmitter::output();" << endl;
    #define out(x) cout << #x << ": " << x << endl
    out(x);
    out(y);
    #undef out
}
